package com.csdj.s3project.config;

import com.csdj.s3project.common.CacheConstant;
import com.csdj.s3project.common.WebResult;
import com.csdj.s3project.common.WebResultMsgCode;
import com.csdj.s3project.service.RedisService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {
    private Logger log = LoggerFactory.getLogger("loginInterceptor");

    private RedisService redisService;

    public LoginInterceptor(RedisService redisService){
        this.redisService = redisService;
    }


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        String uid = request.getHeader(CacheConstant.HEADER_UID);
//        if(StringUtils.hasText(uid)){
//            String token = request.getHeader(CacheConstant.HEADER_TOKEN);
//            if(StringUtils.hasText(token)){
//                log.info("拦截器生效");
//                String cacheToken = redisService.getString(CacheConstant.TOKEN_PREFIX+uid);
//                if(token.equals(cacheToken)){
                    return true;
//                }
//            }
        }
//        response.setCharacterEncoding("utf-8");
//        response.setContentType("application/json;charset=utf-8");
//        WebResult noLogin = new WebResult(WebResultMsgCode.USER_NO_LOGIN);
//        ObjectMapper objectMapper = new ObjectMapper();//json
//        response.getWriter().write(objectMapper.writeValueAsString(noLogin));
//        response.flushBuffer();
//        return false;
//    }


}
